<?php

namespace common\widgets;

use Yii;
use yii\base\Widget;
use yii\widgets\Pjax;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use backend\assets\FastSearchAsset;
use yii\helpers\Html;

/**
 * FastSearch displays a list of items indicating the position of the current page in the whole site hierarchy.
 *
 * ```php
 * // $this is the view object currently being used
 * echo FastSearch::widget([
 *     'items' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
 * ]);
 * ```
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class FastSearch extends Widget {

    /**
     * @var string the name of the breadcrumb container tag.
     */
    public $tag = 'div';

    /**
     * @var array the HTML attributes for the breadcrumb container tag.
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
     */
    public $options = [
        'options' => [
            'class' => 'portlet-input input-inline'
        ]
    ];

    /**
     * @var boolean whether to HTML-encode the item labels.
     */
    public $placeholder = 'Search...';

    /**
     * @var array list of items to appear in the breadcrumbs. If this property is empty,
     * the widget will not render anything. Each array element represents a single item in the breadcrumbs
     * with the following structure:
     */
    public $searchModel = '';
    public $field = 'keyword';
    public $scope = [];

    /**
     * Renders the widget
     */
    public function run() {
        if (!($this->searchModel instanceof \common\db\ActiveRecord)) {
            return;
        }
        $model = $this->searchModel;
        if (method_exists($model, 'loadKeywordOptions')) {
            $keywordOptions = $model::loadKeywordOptions();
            $this->field = ArrayHelper::getValue($keywordOptions, 'field');
            $this->placeholder = ArrayHelper::getValue($keywordOptions, 'placeholder');
        }
        if (!isset($this->options['id'])) {
            $this->options['id'] = $this->getId();
        }
        echo Html::beginTag('div', $this->options['options']);
        $this->renderForm($model);
        echo Html::endTag('div');
        $this->registerAssets();
    }

    public function renderForm($model) {
        $route = [
            $this->getView()->context->module->id,
            $this->getView()->context->id,
            $this->getView()->context->action->id,
        ];
        $form = ActiveForm::begin([
            'method' => 'get',
            'action' => '/' . implode('/', $route),
            'options' => [
                'data-pjax' => true,
            ],
            'fieldConfig' => [
                'template' => '{hint}{input}',
                'options' => [
                    'class' => 'input-icon right'
                ],
            ]
        ]);
        echo $form->field($model, $this->field)->textInput([
            'class' => 'form-control',
            'placeholder' => $this->placeholder
        ])->hint(' ', ['class' => 'btn-fastsearch icon-magnifier', 'tag' => 'button', 'type' => 'submit']);
        ActiveForm::end();
    }

    /**
     * Registers the needed assets
     */
    public function registerAssets() {
        $view = $this->getView();
        FastSearchAsset::register($view);
        $view->registerJs('$("#' . $this->options['id'] . '").on("pjax:end", function() {
            $.pjax.reload({container:"#keywordfilter"}); 
        });');
    }

}
